package cn.itsource.org.service.impl;

import cn.itsource.basic.service.BaseServiceImpl;
import cn.itsource.basic.util.PageList;
import cn.itsource.basic.util.RedisService;
import cn.itsource.basic.util.TreeUtil;
import cn.itsource.org.domain.Department;
import cn.itsource.org.mapper.DepartmentMapper;
import cn.itsource.org.query.DepartmentQuery;
import cn.itsource.org.service.IDepartmentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service //交给spring容器管理
@Transactional //强制事务
public class DepartmentServiceImpl extends BaseServiceImpl<Department,DepartmentQuery> implements IDepartmentService {

    /**
     * 注入mapper层
     */
    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public PageList<Department> findPage(DepartmentQuery departmentQuery) {
        //1.分页插件
        PageHelper.startPage(departmentQuery.getCurrentPage(), departmentQuery.getPageSize());

        //2.查询所有
        List<Department> departments = departmentMapper.findPage(departmentQuery);

        if (departments.size() == 0){
            return new PageList<>();
        }

        //3.封装成一个PageInfo对象 插件提供的
        PageInfo<Department> pageInfo = new PageInfo<>(departments);

        //4.返回分页结果
        return new PageList<>(pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * 抽取方法
     *   alt + shift + M
     */
    @Override
    public void saveOrUpdate(Department department) {
        //没有id就是新增，有id就是else 修改
        if (department.getId() == null) {
            departmentMapper.insertSelective(department);

            //调用抽取好的方法alt + shift + M
            createData(department);
            departmentMapper.updateByPrimaryKey(department);
        }else {


            //调用抽取好的方法alt + shift + M
            createData(department);

            departmentMapper.updateByPrimaryKey(department);
        }
    }

    private void createData(Department department) {
        Long[] parentIds = department.getParentIds();// [3,8,10]

        StringBuilder dirPath = new StringBuilder("");
        //有上级部门的情况
        if (parentIds != null && parentIds.length > 0) {
            //目的 入库：parentId:10  dirPath:/3/8/10/自增id
            Long parentId = parentIds[parentIds.length - 1];

            department.setParentId(parentId);


            for (Long id : parentIds) {
                dirPath.append("/").append(id);
            }
        } else {
            department.setParentId(null);
        }
        dirPath.append("/").append(department.getId());

        department.setDirPath(dirPath.toString());
    }


    @Override
    public List<Department> treeDept() {

        //1.查询所有
        List<Department> departments = departmentMapper.selectAll();
        //2.调用工具类
        List<Department> list = TreeUtil.getTree(departments);
        //3.返回结果
        return list;
    }

    @Override
    public List<Department> parentDepts() {
        return departmentMapper.getParentDepts();
    }

}
